package yxy.game.pm2.active.module;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actgradepackage;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Map;

public class ActiveModule_等级礼包 extends PlayerModule {

	//bean -------------------------------------------------
	private static final int acttype = 10;
	
	private ArrayList<Integer> drawids = new ArrayList<Integer>();
	
	public ArrayList<Integer> getDrawids() {
		return drawids;
	}
	public void setDrawids(ArrayList<Integer> drawids) {
		this.drawids = drawids;
	}
	
	//func --------------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
//	public static final int getover = 4;
	
	private static ActiveModule_等级礼包 create(String playercode) {
		ActiveModule_等级礼包 am = new ActiveModule_等级礼包();
		am.setPlayerCode(playercode);
		am.set();
		return am;
	}
	
	public void update() {
		
	}
	
	//redis --------------------------------------------------
	
	static private String key(String playercode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playercode, acttype);
	}

	public void set() {
		try {
			String key1 = key(getPlayerCode());
			setv(key1, this);
			check();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_等级礼包
	 * 
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_等级礼包 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_等级礼包 module = getv(key(playerCode));

			//新用户不需要去查数据库
			if (module == null && !isNewPlayer) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_等级礼包.class);
				if(module != null){module.set();}//同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			if(module == null){return null;}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		Map<String, actgradepackage> agpmap = ActDataCenter.getAllData(actgradepackage.class, getPlayerCode());
		
		if(getDrawids().size() >= agpmap.size()) {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.领完了);
		}else {			
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		}
	
		PlayerBean pb = PlayerBean.get(getPlayerCode());
		int level = pb.getLevel();
		
		for(actgradepackage data : agpmap.values()) {
			if(getDrawids().contains(data.getId())) {
				continue;
			}
			
			if(level >= data.getCondition()) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_等级礼包奖励, true);
				return;
			}
		}
		new TipHandle().updateTip(getPlayerCode(), TipType.活动_等级礼包奖励, false);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_等级礼包 activeModule_等级礼包 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_等级礼包);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
	
	
}
